﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.Model;
using Project.Module.Model.CMS;
using Project.Engine.Utilities;

namespace Project.Module.DAO.BL
{
    public class ImageBL : DAO.IBL.IBLTable<Image, int>
    {
        public void AddNewImageProduct(string image, int productId)
        {
            Image img = new Image { 
                ImagePath = image,
                CategoryID = productId,
            };
            this.Context.Images.InsertOnSubmit(img);
            this.Context.SubmitChanges();
        }

        public int GetImageIdByProductId(int productId) {
            var query = from e in this.Context.Images
                        where e.CategoryID == productId
                        select new { id = e.ImageID };
            if (query != null)
            {
                return ConvertUtil.ToInt32(query.Select(c => c.id).FirstOrDefault());
            }

            return 0;
        }

        public string GetImagePathById(int imageId)
        {
            string path = String.Empty;
            var query = from e in this.Context.Images
                        where e.ImageID == imageId
                        select new { ImagePath = e.ImagePath, };
            if (query != null)
            {
                path = Convert.ToString(query.Select(c => c.ImagePath).FirstOrDefault().ToString());
            }

            return path;
        }
    }
}
